package cn.turing.firecontrol.device.util;

import cn.turing.firecontrol.device.entity.DeviceSensorDo;
import cn.turing.firecontrol.device.entity.ExcelColumn;
import cn.turing.firecontrol.device.entity.ExcelDate;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * @auther Wyt
 * @date 2023/4/7/007 10:04
 */
public class EasyExcelUtil {
    public static void writeDevicesToExcel(String filePath, List<DeviceSensorDo> devices) throws Exception {
        // 创建工作簿对象
        SXSSFWorkbook workbook = new SXSSFWorkbook(100);
        // 设置每次写入内存的行数，默认为100
       // workbook.setRowAccessWindowSize(1000);

        // 创建工作表对象
        Sheet sheet = workbook.createSheet("Sheet1");

        // 写入表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Device ID");
        headerRow.createCell(1).setCellValue("Device buildingId");
        headerRow.createCell(2).setCellValue("Device channelId");
        headerRow.createCell(3).setCellValue("Device sensorNo");
        headerRow.createCell(4).setCellValue("Device floor");
        headerRow.createCell(5).setCellValue("Device positionDescription");
        headerRow.createCell(7).setCellValue("Device equipmentType");
        headerRow.createCell(6).setCellValue("Device manufacturer");
        headerRow.createCell(8).setCellValue("Device model");
        headerRow.createCell(9).setCellValue("Device Status");

        // 写入数据
        int rowIndex = 1;
        for (DeviceSensorDo device : devices) {
            Row row = sheet.createRow(rowIndex++);
            row.createCell(0).setCellValue(device.getId());
            row.createCell(1).setCellValue(device.getBuildingId());
            row.createCell(2).setCellValue(device.getChannelId());
            row.createCell(3).setCellValue(device.getSensorNo());
            row.createCell(4).setCellValue(device.getFloor());
            row.createCell(5).setCellValue(device.getPositionDescription());
            row.createCell(6).setCellValue(device.getEquipmentType());
            row.createCell(7).setCellValue(device.getManufacturer());
            row.createCell(8).setCellValue(device.getModel());
        }

        // 保存工作簿到文件
        FileOutputStream outputStream = new FileOutputStream(filePath);
        workbook.write(outputStream);
        workbook.dispose(); // 释放临时文件并关闭文件流
        outputStream.close();
    }



























    /**
     * 导出
     *
     * @param
     * @return null
     */
   /* public static void simpleWrite(RedisTemplate redisTemplate) {

        // ...填充数据集合
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("设备传感器表.xlsx");

            writeExcel(redisTemplate, DeviceSensorDo.class, outputStream);
            outputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }*/
   /* public static <T> void writeExcel(ArrayList<DeviceSensorDo> list, Class<T> clazz, OutputStream outputStream)  {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // 获取字段列表
        List<Field> fieldList = Arrays.asList(clazz.getDeclaredFields());

        // 写入表头
        Row headerRow = sheet.createRow(0);
        int columnIndex = 0;
        for (Field field : fieldList) {
            ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
            if (annotation != null) {
                String columnName = annotation.value();
                Cell cell = headerRow.createCell(columnIndex++);
                cell.setCellValue(columnName);
            }
        }

        // 写入数据
        int rowIndex = 1;
       // ArrayList<DeviceSensorDo> deviceSensorDos = (ArrayList<DeviceSensorDo>) redisTemplate.opsForValue().get("deviceSensorDos1");
        for (DeviceSensorDo data : list) {
            Row row = sheet.createRow(rowIndex++);
            columnIndex = 0;
            for (Field field : fieldList) {
                ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
                if (annotation != null) {
                    Cell cell = row.createCell(columnIndex++);
                    field.setAccessible(true);
                    Object value = null;
                    try {
                        value = field.get(data);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    if (value != null) {
                        if (field.getType() == DeviceSensorDo.class) {
                            ExcelDate dateAnnotation = field.getAnnotation(ExcelDate.class);
                            if (dateAnnotation != null) {
                                String pattern = dateAnnotation.value();
                                SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
                                cell.setCellValue(dateFormat.format((Date) value));
                            }
                        } else {
                            cell.setCellValue(value.toString());
                        }
                    }
                }
            }
        }
        System.err.println("关闭流");
        try {
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
*/
}
